//着色器代码

	
	const vertice_firework_source = `#version 300 es
	precision mediump float;
	in vec2 aPos;
in vec3 instance_direction;

out vec4 firework_color;

uniform mat4 pers_and_rotate;
uniform float time_fire;
//uniform mat4 rota;
uniform mat4 rota_c;//逆元操作

uniform vec3 color_firework;
uniform float rand_velocity;
uniform float rand_position;
uniform float rand_explode;
uniform float size;//用于渲染轨迹时使用
uniform float seed;//用于方向与速度发生随机偏移

float pos_in_g(float velocity_0, float g, float time){//重力计算
	return (2.0 * velocity_0 - g * time) * time / 2.0;
}

float pos_in_g_drag(float velocity_0, float g, float time, float drag){//有阻力情况下计算
	float tmp_c = velocity_0 - g / drag;

	return tmp_c * (exp(drag * time) - 1.0) / (drag) + g / drag *  time;
}

void main()
{
	vec3 pos_firework = vec3(aPos.xy * 0.005 * size, 0);
	vec3 pos_offset = vec3(0,0,0);
	
	float velocity_up = 0.5 + rand_velocity * 0.2;//初始速度
	const float time_up = 1.0;//上升时间
	const float g = 0.3;//重力加速度
	const float drag = -2.0;//空气阻力系数
	float height = pos_in_g(velocity_up, g, time_up);//最大高度
	
	float amp_explode = 0.4 + rand_explode * 0.2;//爆炸力度
	vec3 explode_velo = instance_direction * amp_explode;//爆炸初速度

	//随机种子处理
	float rand_num = seed * instance_direction.x * instance_direction.z;
	vec3 rand_offset;
	rand_offset.x = rand_num - floor(rand_num);
	rand_offset.y = rand_offset.x * 10.0 - floor(rand_offset.x * 10.0);
	rand_offset.z = rand_offset.y * 10.0 - floor(rand_offset.y * 10.0);
	explode_velo += (vec3(rand_offset) - vec3(0.5)) * 0.2;
	

	//判断状态
	if(time_fire < 1.0){
		pos_offset = vec3(0.0, pos_in_g(velocity_up, g, time_fire), 0.0);
	}else{
		float time_tmp = time_fire - 1.0;
		
		//pos_offset = vec3(explode_velo.x * time_tmp, pos_in_g(explode_velo.z, g, time_tmp) + height, explode_velo.y * time_tmp);
		pos_offset = vec3(
		explode_velo.x * (exp(drag * time_tmp) - 1.0) / (drag), 
		pos_in_g_drag(explode_velo.z, g, time_tmp, drag) + height,
		explode_velo.y * (exp(drag * time_tmp) - 1.0) / (drag)
		);

		pos_firework *= 1.0 - pow(time_tmp, 2.0);//随时间推移 大小会降低

	}
	//加入随机偏移
	gl_Position = pers_and_rotate * (vec4(pos_offset, 1.0) + rota_c * vec4(pos_firework, 0) + vec4(2.0 * rand_position - 1.0, -0.2, -1.0, 0));
	
	//颜色
	firework_color = vec4(color_firework, 1.0);//hdr
	
}
	`;
	const fragment_firework_source = `#version 300 es
	precision mediump float;
	in vec4 firework_color;
	out vec4 FragColor;
	void main(){
		FragColor = firework_color;
	}
	`;
	
	
//默认缓冲用于渲染纹理的着色器
	const vertice_rect_source = `#version 300 es
	in vec4 aPos_n_texcoord;//坐标和纹理坐标
	out vec2 coord_tex;
	void main(){
		gl_Position = vec4(aPos_n_texcoord.xy, 0, 1);
		coord_tex = aPos_n_texcoord.zw;
	}
	`;
	const fragment_rect_source =`#version 300 es
	precision mediump float;
	in vec2 coord_tex;
	out vec4 FragColor;

	uniform sampler2D Tex_hdr;
	uniform sampler2D Tex_blur;
	void main(){
		FragColor =  vec4(texture(Tex_hdr, coord_tex).xyz + texture(Tex_blur, coord_tex).xyz, 1);
	}
	`;


//blur 着色器
const fragment_blur_downsample_source =`#version 300 es
	precision mediump float;
	in vec2 coord_tex;
	out vec4 FragColor;

	uniform vec2 span_pixel;
	uniform sampler2D Tex_hdr;
	void main(){
		vec3 sum = texture(Tex_hdr, coord_tex).xyz * 4.0;

		sum += texture(Tex_hdr, coord_tex + vec2(span_pixel.x, span_pixel.y)).xyz;
		sum += texture(Tex_hdr, coord_tex + vec2(-span_pixel.x, -span_pixel.y)).xyz;
		sum += texture(Tex_hdr, coord_tex + vec2(-span_pixel.x, span_pixel.y)).xyz;
		sum += texture(Tex_hdr, coord_tex + vec2(span_pixel.x, -span_pixel.y)).xyz;

		FragColor =  vec4(sum * 0.125, 1.0);

	}
	`;

const fragment_blur_upsample_source =`#version 300 es
	precision mediump float;
	in vec2 coord_tex;
	out vec4 FragColor;

	uniform vec2 span_pixel;
	uniform sampler2D Tex_hdr;
	void main(){
		vec3 sum = vec3(0.0);

		sum += texture(Tex_hdr, coord_tex + vec2(span_pixel.x, span_pixel.y)).xyz * 2.0;
		sum += texture(Tex_hdr, coord_tex + vec2(-span_pixel.x, -span_pixel.y)).xyz * 2.0;
		sum += texture(Tex_hdr, coord_tex + vec2(-span_pixel.x, span_pixel.y)).xyz * 2.0;
		sum += texture(Tex_hdr, coord_tex + vec2(span_pixel.x, -span_pixel.y)).xyz * 2.0;
		sum += texture(Tex_hdr, coord_tex + vec2(span_pixel.x * 2.0, 0.0)).xyz;
		sum += texture(Tex_hdr, coord_tex + vec2(-span_pixel.x * 2.0, 0.0)).xyz;
		sum += texture(Tex_hdr, coord_tex + vec2(0.0, span_pixel.y * 2.0)).xyz;
		sum += texture(Tex_hdr, coord_tex + vec2(0.0, -span_pixel.y * 2.0)).xyz;

		FragColor =  vec4(sum * 0.0833, 1.0);

	}
	`;